/**
 * 
 */
package com.jytc.framework.commons;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.jytc.framework.dto.FwPermissionDTO;
import com.jytc.framework.po.FwRole;
import com.jytc.framework.po.FwUser;

/**
 * 
 * @title: 后台管理系统登录令牌类
 * @description: 在后台用户登录后，该用户的所有信息将通过该对象保存至用户会话中
 * 
 * @author: dongyuese
 * 
 */
@Scope("prototype")
@Component
public class AdminLoginToken
{
	// 登录信息
	private FwUser fwUser;

	// 角色信息
	private List<FwRole> fwRoles;

	// 菜单权限
	private Map<Integer, String> menuPermissions;

	private List<FwPermissionDTO> allMenus;

	public FwUser getFwUser() {
		return fwUser;
	}

	public void setFwUser(FwUser fwUser) {
		this.fwUser = fwUser;
	}

	public List<FwRole> getFwRoles() {
		return fwRoles;
	}

	public void setFwRoles(List<FwRole> fwRoles) {
		this.fwRoles = fwRoles;
	}

	public Map<Integer, String> getMenuPermissions()
	{
		return menuPermissions;
	}

	public void setMenuPermissions(Map<Integer, String> menuPermissions)
	{
		this.menuPermissions = menuPermissions;
	}

	/**
	 * 获得菜单树XML
	 * @return
	 */
	public String getMenuTreeXml()
	{
		String treeXml = "";
		try
		{
			Tree menuTree = new Tree();
			//所有菜单

			for (FwPermissionDTO fwPermissionDTO : allMenus)
			{
				TreeNode node = new TreeNode(String.valueOf(fwPermissionDTO.getPermissionId()));
				node.setText(fwPermissionDTO.getPermissionName());
				node.setParentId(String.valueOf(fwPermissionDTO.getParentPermissionId()));
				if (fwPermissionDTO.getPermissionUrl() != null)
					node.addUserData("url", fwPermissionDTO.getPermissionUrl());
				menuTree.addNode(node);
			}
			//菜单权限
			List<String> permissionsMenuIds = new ArrayList<String>();

			for (Integer menuId : menuPermissions.keySet())
			{
				permissionsMenuIds.add(String.valueOf(menuId));
			}
			//将除了该用户菜单权限以外的菜单都隐藏
			menuTree.setExpectNodeHidden(permissionsMenuIds);
			treeXml = menuTree.getXml();
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return treeXml;
	}

	public void setAllMenus(List<FwPermissionDTO> allMenus)
	{
		this.allMenus = allMenus;
	}

}
